package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dao.BookDao;
import com.itheima.daomain.Book;
import com.itheima.service.BookService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: 宋H
 * @Date: 2022/8/19
 * @Description: com.itheima.service.impl
 * @version: 1.0
 */
@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookDao bookDao;

    @Override
    public Boolean insert(Book b) {
        return bookDao.insert(b) > 0;
    }

    @Override
    public Book getId(Integer id) {
            return bookDao.selectById(id);
    }

    @Override
    public List<Book> getAll() {
        return bookDao.selectList(null);
    }

    @Override
    public Boolean update(Book b) {
        return bookDao.updateById(b) > 0;
    }

    @Override
    public Boolean delete(Integer id) {
        return bookDao.deleteById(id) > 0;
    }

    @Override
    public IPage<Book> getPage(int currentPage, int pageSize) {
        IPage page = new Page(currentPage,pageSize);
        bookDao.selectPage(page,null);
        return page;
    }

    @Override
    public IPage<Book> getPage(int currentPage, int pageSize, Book book) {
        LambdaQueryWrapper<Book> blw = new LambdaQueryWrapper<Book>();
        blw.like(Strings.isNotEmpty(book.getType()),Book::getType,book.getType());//判定是否为空后在做like选择查询
        blw.like(Strings.isNotEmpty(book.getName()),Book::getName,book.getName());
        blw.like(Strings.isNotEmpty(book.getTel()),Book::getTel,book.getTel());
        IPage page = new Page(currentPage,pageSize);
        bookDao.selectPage(page,blw);
        return page;
    }
}
